﻿using Iworld.Author.Model;
using Iworld.basis.Service;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Service.Backstage.Import
{
    /// <summary>
    /// 编辑用户数据集
    /// </summary>
    [DataContract]
    public class EditUserImport : CheckAdministratorImportBase, IPackageForUpdateModel<IModelToDbContextOfAuthor, Iworld.Author.Model.Author>
    {

        [DataMember]
        public int UserId { set; get; }

        [DataMember]
        public AuthorStatus? NewStatus { set; get; }

        [DataMember]
        public string NewPassword { set; get; }

        public int Id
        {
            get { return UserId; }
        }

        public override void CheckAllowExecuteOrNot(Main.Impl.ModelToDbContext db)
        {
            base.CheckAllowExecuteOrNot(db);
            if (!Self.Group.CanEditUser)
            {
                throw new Exception("你无权修改用户");
            }
        }

        public void CheckData(IModelToDbContextOfAuthor db)
        {
            if (!string.IsNullOrWhiteSpace(NewPassword))
            {
              VerifyHelper.Check(this.NewPassword, VerifyHelper.Key.Password);
            }
        }

        public void Update(Model.Author model)
        {
            if(NewStatus!=null)
            {
                model.Status = (AuthorStatus)NewStatus;
            }
            if (!string.IsNullOrWhiteSpace(NewPassword))
            {
                model.Password = EncryptHelper.EncryptByMd5(this.NewPassword);
            }
        }
    }
}
